/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.knolle.vista;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import modelo.Lotes;
import org.knolle.modelo.LotesController;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;


/**
 * Top component which displays something.
 */
@ConvertAsProperties(
    dtd="-//org.knolle.vistacampaña//VistaAgregarLotes//EN",
    autostore=false
)
@TopComponent.Description(
    preferredID="VistaAgregarLotesTopComponent", 
    //iconBase="SET/PATH/TO/ICON/HERE", 
    persistenceType=TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "properties", openAtStartup = false)
@ActionID(category = "Window", id = "org.knolle.vistacampaña.VistaAgregarLotesTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
    displayName = "#CTL_VistaAgregarLotesAction",
    preferredID="VistaAgregarLotesTopComponent"
)
@Messages({
    "CTL_VistaAgregarLotesAction=VistaAgregarLotes",
    "CTL_VistaAgregarLotesTopComponent=VistaAgregarLotes Window",
    "HINT_VistaAgregarLotesTopComponent=This is a VistaAgregarLotes window"
})
public final class VistaAgregarLotesTopComponent extends TopComponent {
    
    //Variables de Marcelo
//    ControllerCampañas controllerCampañas = new ControllerCampañas();
    
    public VistaAgregarLotesTopComponent() {
        initComponents();
        setName(Bundle.CTL_VistaAgregarLotesTopComponent());
        setToolTipText(Bundle.HINT_VistaAgregarLotesTopComponent());
	putClientProperty(TopComponent.PROP_MAXIMIZATION_DISABLED, Boolean.TRUE);
        popularList();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jProgressBar1 = new javax.swing.JProgressBar();

        jList1.setModel(modeloList1);
        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setViewportView(jList2);

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, "Agregar");

        org.openide.awt.Mnemonics.setLocalizedText(jButton2, "Agregar Todos");

        org.openide.awt.Mnemonics.setLocalizedText(jButton3, "Borrar");

        org.openide.awt.Mnemonics.setLocalizedText(jButton4, "Borrar Todos");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jButton3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jProgressBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addComponent(jProgressBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4)
                        .addGap(0, 202, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JProgressBar jProgressBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }
    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
    
    
    private ListDataModel modeloList1 = new ListDataModel();
    private DefaultListModel modeloList2 = new DefaultListModel();
    
    private void popularList(){
        List<Lotes> lotesIncluidos = LotesController.getListaLotes();
//        List<Lotes> lotesNoIncluidos ;
//        modeloList1.ensureCapacity(lotesIncluidos.size() +1);
        for (Lotes lote: lotesIncluidos){
         modeloList1.addElement(lote);
         
        }
//        for (Integer i=0; i<20 ; i++){
//         modeloList2.add(i, "Chau ->"+i.toString());
//        }
        
        jList1.setDragEnabled(true);
        jList2.setDragEnabled(true);
        jList1.setDropMode(DropMode.ON);
        jList2.setDropMode(DropMode.ON);
        
       
    }
    
    
    //Manejador para la transferencia de objetos pojos del tipo Lote
//    private class ListTransferHandler extends TransferHandler {
//        
//            
//    
//    }
    
    private class ListDataModel extends AbstractListModel {

        List<Lotes> listaLotes = new ArrayList<Lotes>();
        
        @Override
        public int getSize() {
            
            if (listaLotes == null){
                return -1;
            }
            return listaLotes.size();
        }

        @Override
        public Object getElementAt(int index) {
            if (listaLotes.get(index) != null){
               return listaLotes.get(index);
            }
            return null;
        }
        
        public void add(Lotes element){
            listaLotes.add(element); 
        }
        public void addElement(Lotes element){
            listaLotes.add(element); 
        }
        
        public void remove(int index){
            listaLotes.remove(index);
        }
        
        public void remove(Lotes element){
            listaLotes.remove(element);     
        }
    
    }
}
